package com.vueperson.vueperson.tools;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;




public class DateSets {
	//public static String sql="select * from dxtestwork where  sn=?";
	//public static String sql="select * from dxtestwork where username=? and sn=?";
    
	
	public static String nowdate() {
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
		String dateString=dateFormat.format(new Date());
		return dateString;
	}
	public static String tomorrow() {
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar=new GregorianCalendar();
		calendar.setTime(new Date());
		calendar.add(calendar.DATE, 1);
		String dateTomString=dateFormat.format(calendar.getTime());
		return dateTomString;
	}
	public static String anytomorrow(String n) {
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
		String dateTomString="";
		try {
		Date dd=dateFormat.parse(n);
		Calendar calendar=new GregorianCalendar();
		calendar.setTime(dd);
		calendar.add(calendar.DATE, 1);
		 dateTomString=dateFormat.format(calendar.getTime());
		
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dateTomString;
	}
	public static String yesterdate() {
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar=new GregorianCalendar();
		calendar.setTime(new Date());
		calendar.roll(calendar.DATE, -1);
		String dateTomString=dateFormat.format(calendar.getTime());
		return dateTomString;
	}



	public static String monthNow(Date n) {
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar=new GregorianCalendar();
		calendar.setTime(n);
		calendar.set(Calendar.DAY_OF_MONTH,1);
		calendar.roll(calendar.MONTH, 0);
		String dateTomString=dateFormat.format(calendar.getTime());
		return dateTomString;
	} 
	public static String monthNext(Date n) {
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar=new GregorianCalendar();
		calendar.setTime(n);
		calendar.set(Calendar.DAY_OF_MONTH,1);
		calendar.roll(calendar.MONTH, 1);
		String dateTomString=dateFormat.format(calendar.getTime());
		return dateTomString;
	}


	/**
	 * 给起始日期和给定月份，计算结束日期工具
	 * 开始日期格式为：yyyy-MM-dd
	 * */
	public static String getEndDateBymonths(Date date, int months){
		SimpleDateFormat sj = new SimpleDateFormat("yyyy-MM-dd");
		//分隔日期为字符串数组，1为年，2为月，3为日
		String startDateStr =sj.format(date);
		String dataStr[] = startDateStr.split("-");
		//年份,开始日期的月份加上当前月份。除以12，即增加年数。
		int  year = (Integer.parseInt(dataStr[1]) + months)/12;
		//月份
		int mon = (Integer.parseInt(dataStr[1]) + months)%12;

		//格式化月份。
		String a = "";
		if(mon<10){
			if(mon<1){
				a = "12";
			}else{
				a = "0"+mon;
			}
		}else {
			a = mon+"";
		}
		dataStr[0]=String.valueOf(Integer.parseInt(dataStr[0]) + year);
		dataStr[1]=a;
		String newdata ="";
		newdata = dataStr[0]+"-"+dataStr[1]+"-"+dataStr[2];
		return newdata;
	}

    

    
    public static String dataJsonNull(){
        String json="[";
        
            String line="{\"message\":\""+""+
            	"\",\"username\":\""+""+
            	"\",\"userpwd\":\""+""+
			  "\",\"sdate\":\""+""+
			  "\",\"edate\":\""+""+
			  "\",\"nowdate\":\""+""+
			 "\",\"sn\":\""+""+
			  "\",\"id\":\""+""+"\"}"+
			 "\r\n";
            json=json+line;
           // System.out.println(8888);
        
       // json=json.substring(0, json.length()-3);
        json=json+"]";
        return json;

    }

    public static  String anyday(Date date, int i,String Type){
		SimpleDateFormat sj = new SimpleDateFormat("yyyy-MM-dd");
//		String newdata ="2018-01-01";
		String newdata =sj.format(date);
		Date d;
		try {
			d = sj.parse(newdata);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(d);
			if (Type.equals("year")){
				calendar.add(Calendar.YEAR, i);
			}if (Type.equals("month")){
				calendar.add(Calendar.MONTH, i);
			}if (Type.equals("week")){
				calendar.add(Calendar.WEDNESDAY, i);
			}
			else {
				calendar.add(Calendar.DATE, i);
			}

			newdata = sj.format(calendar.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
//		System.out.println(newdata);
		return newdata;
	}



public static void main(String[] args) {
	Date mydate = new Date();
	System.out.println(DateSets.anyday(new Date(),-1,"week"));
//	System.out.println(DataSets.getEndDateBymonths(new Date(),14));


}


}
